/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hiddendanger.uitls;

import com.koron.hiddendanger.domain.HiddenDangerDiscovery;
import com.koron.hiddendanger.domain.HiddenDangerRectification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 隐患整改超期判断
 *
 */
public class OverdueJudgmentUtil {
    private final static Logger logger = LoggerFactory.getLogger(OverdueJudgmentUtil.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 整改情况判断,（只判断离期限三天内和超期的）
     * 已提交，已确定的隐患，只要是完成时间或者当前时间超过整改期限或者超过延期时间的，都算作超期
     * 离期限三天内isDelay=1，超期isDelay=2
     *
     * @param danger
     * @param rectification
     */
    public static void setIsDelay3(HiddenDangerDiscovery danger, HiddenDangerRectification rectification) {
        try {
            // 草稿，未确定和整改完成的数据，不加判断
            if (danger.getState() != 0 && danger.getState() != 1  && danger.getState() != 7) {
                //未延期，或延期驳回
                if (rectification.getDelayState() != 2) {
                    if (rectification.getDeadline() != null) {
                        if (rectification.getCompleteTime() != null) {
                            // 超期完成，isDeay=2
                            Date compDate = dateFormat.parse(dateFormat.format(rectification.getCompleteTime()));
                            if (compDate.compareTo(rectification.getDeadline()) == 1) {
                                danger.setIsDelay(2);
                            }
                        } else {
                            Calendar calendar = Calendar.getInstance();
                            Date date = dateFormat.parse(dateFormat.format(calendar.getTime()));
                            // 超期未完成，isDeay=2
                            if (date.after(rectification.getDeadline())) {
                                danger.setIsDelay(2);
                            } else {
                                // 离期限不足三天，isDelay=1
                                calendar.add(Calendar.DAY_OF_MONTH, 3);
                                Date time = dateFormat.parse(dateFormat.format(calendar.getTime()));
                                if (time.after(rectification.getDeadline())) {
                                    danger.setIsDelay(1);
                                }
                            }
                        }
                    }
                } else {
                    //延期
                    if (rectification.getDelayDate() != null) {
                        if (rectification.getCompleteTime() != null) {
                            Date compDate = dateFormat.parse(dateFormat.format(rectification.getCompleteTime()));
                            if (compDate.compareTo(rectification.getDelayDate()) == 1) {
                                danger.setIsDelay(2);
                            }
                        } else {
                            Calendar calendar = Calendar.getInstance();
                            Date date = dateFormat.parse(dateFormat.format(calendar.getTime()));
                            // 超期未完成，isDeay=2
                            if (date.after(rectification.getDelayDate())) {
                                danger.setIsDelay(2);
                            } else {
                                calendar.add(Calendar.DAY_OF_MONTH, 3);
                                Date time = dateFormat.parse(dateFormat.format(calendar.getTime()));
                                if (time.after(rectification.getDelayDate())) {
                                    danger.setIsDelay(1);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }
}
